	global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/table_3_alm_main.log, replace name(tabl)

capture noi {

	* table_alm_main

	* Configuration
	global cvers "_ipc4"
	local min_pats 0


	capture program drop addMean
	program define addMean
		qui sum `1' [aw=avwt]
		local x: di %10.2f `r(mean)'
		estadd local weighted_mean `x'
	end
	if (`min_pats' != 0) local restr "_min`min_pats'pats"

	use ${final_dir}/ind6090_patents_regression_data${cvers}.dta, clear

	***** Set 1: Share auto using only

	keep if in_relevant_field_use >= `min_pats'
	*column 1, routine congitive delta, share auto using, no share auto inventing
	reg dqsts sh_auto95_use computeruse manuf [aw=avwt], vce(cluster uas_ind6090) 
	addMean dqsts
	estadd local obs "{\num{`e(N)'}}"
	di "industries using: `e(N)'"
	di "clusters using: `e(N_clust)'"

	estimates store cog_set1
	*column 3, routine manual delta, share auto using, no share auto inventing
	reg dqfing sh_auto95_use computeruse manuf [aw=avwt], vce(cluster uas_ind6090) 
	addMean dqfing
	estadd local obs "{\num{`e(N)'}}"
	estimates store man_set1

	*column 5, High vs low delta delta, share auto using, no share auto inventing
	reg dratioHL sh_auto95_use computeruse manuf [aw=avwt], vce(cluster uas_ind6090) 
	addMean dratioHL
	estadd local obs "{\num{`e(N)'}}"
	estimates store hlr_set1

	*column 7, labor share NBER delta, share auto using, no share auto inventing
	reg d_lshare_nber sh_auto95_use computeruse [aw=avwt], vce(cluster uas_ind6090) 
	addMean d_lshare_nber
	estadd local obs "{\num{`e(N)'}}"
	estimates store ls_set1

	use ${final_dir}/beaind_patents_regression_data${cvers}.dta, clear

	*column 9, labor share BEA delta, share auto using, no share auto inventing
	reg d_lshare sh_auto95_use computeruse manuf [aw=emp_mid_alm], vce(cluster uas_beaind) 
	addMean d_lshare
	estadd local obs "{\num{`e(N)'}}"
	estimates store lsbea_set1
	di "industries using (bea): `e(N)'"
	di "clusters using (bea): `e(N_clust)'"

	*****  Set 2: Share auto using + making
	use ${final_dir}/ind6090_patents_regression_data${cvers}.dta, clear

	keep if in_relevant_field_use >= `min_pats'
	keep if in_mk == 1

	*column 2, routine congitive delta, share auto using and share auto inventing
	reg dqsts sh_auto95_use sh_auto95_mk computeruse manuf [aw=avwt], vce(cluster uas_ind6090) 
	addMean dqsts
	estadd local obs "{\num{`e(N)'}}"
	estimates store cog_set2
	di "industries using + inventing: `e(N)'"
	di "clusters using + inventing: `e(N_clust)'"

	*column 4, routine manual delta, share auto using and share auto inventing
	reg dqfing sh_auto95_use sh_auto95_mk computeruse manuf [aw=avwt], vce(cluster uas_ind6090) 
	addMean dqfing
	estadd local obs "{\num{`e(N)'}}"
	estimates store man_set2

	*column 6, high vs low delta, share auto using and share auto inventing
	reg dratioHL sh_auto95_use sh_auto95_mk computeruse manuf [aw=avwt], vce(cluster uas_ind6090) 
	addMean dratioHL
	estadd local obs "{\num{`e(N)'}}"
	estimates store hlr_set2

	*column 8, labor share NBER delta, share auto using and share auto inventing
	reg d_lshare_nber sh_auto95_use sh_auto95_mk computeruse [aw=avwt], vce(cluster uas_ind6090) 
	addMean d_lshare_nber
	estadd local obs "{\num{`e(N)'}}"
	estimates store ls_set2

	use ${final_dir}/beaind_patents_regression_data${cvers}.dta, clear

	*column 10, labor share BEA delta, share auto using and share auto inventing
	reg d_lshare sh_auto95_use sh_auto95_mk computeruse manuf [aw=emp_mid_alm], vce(cluster uas_beaind) 
	di "clusters: `e(N_clust)'"
	addMean d_lshare
	estadd local obs "{\num{`e(N)'}}"
	estimates store lsbea_set2

	di "industries using + inventing (bea): `e(N)'"
	di "clusters using + inventing (bea): `e(N_clust)'"

	* Output table
	do ${code_dir}/config/tabletools.do
	setlabels
	dhoztab cog_set1 cog_set2 man_set1 man_set2 hlr_set1 hlr_set2 ls_set1 ls_set2 lsbea_set1 lsbea_set2 using ${tab_dir}/main/Table_3_alm`restr'_main.tex,  b(%10.2f) se(%10.2f) nogaps  replace nolines numbers nonotes nolines showdepvarinfo nomtitles sfmt(%10.2f a1) scalars("r2 \noalign{\vskip 1.8mm} R$^2$" "obs Industries") label mgroups("$\Delta$ Routine cognitive" "$\Delta$ Routine manual" "\makecell[tc]{$\Delta$ High/low \\ skill workers}" "\makecell[tc]{$\Delta$ Labor share \\ (NBER)}" "\makecell[tc]{$\Delta$ Labor share \\ (BEA)}", pattern(1 0  1 0  1 0 1 0 1 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) drop(_cons) noobs order(sh_auto_use sh_auto_mk computeruse manuf) ///
	rename(sh_auto95_use sh_auto_use sh_auto95_mk sh_auto_mk) /// 
		depvar("none")  ///
		notes("This table shows the effect of automation technologies on tasks, skill composition, and the labor share. Each column is a separate OLS regression of the change in an industry outcome between 1980 and 1998 on the share of automation patents in machinery in 1980-1998, the annual percentage point change in industry computer use during 1984-1997, a dummy variable indicating the manufacturing sector, and a constant. In columns 1--2 the dependent variable is the change in routine cognitive tasks and in columns 3--4 the change in routine manual tasks, both measured as 10x the annual change in industry-level task input in centiles of the 1960 task distribution (see ALM). In columns 5--6 the dependent variable is the change in the ratio of high-skill workers (college graduates) over low-skill workers (others). In columns 7--8 and columns 9--10 the dependent variable is the change in the labor share in the NBER-CES manufacturing industry database, and in 60 aggregated industries from the BEA, respectively. As described in the text, the two automation share measures correspond to a different mapping between C/IPC codes and industries. Using industries allocates patents to their sector of use while innovating industry -- added in columns 2, 4, 6, 8, and 10 -- allocates patents to their sector of invention. The regressions are weighted by the mean industry share of total employment in FTEs in 1980 and 1998. Standard errors are clustered at the level of industry groups that have the same automation share by construction and reported in parentheses.")
	*vscode shows the } in the command as wrong, but it works
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close tabl